import {
    searchReq,
    saveReq,
    deleteCenterPriseReq,
    orgCreateBusinessReq,
    refershReq,
    queryCardReq,
    empowerReq
} from "./index.js";
import { vendorsDanger as danger, vendorsLongSuccess as success } from './../../../utils/config.js';
import { buttonClick } from "./buttonClick.js";


function search(params){

    searchReq(params).then((response)=>{
        if(response.data.resultCode != 'busi_ex'){
            response.data.result.map((item, index)=>{
                item["key"] = item.pkEnterprise
            })
            this.setState({
                tableData : response.data.result
            })
            success({"text":'查询数据成功'})
        }else{
            danger({"text":response.data.exMsg});
        }
    })
}

function save(params){

    saveReq(params).then((response)=>{
        if(response.data.resultCode != 'busi_ex'){
            this.setState({
                status: "browse"
            })
            this.props.form.setFormStatus("browse")
            this.props.button.setButtonState("browse")
            this.state.columns.splice(this.state.columns.length-1, 1);
            let  selectOptions = {
                10 : {value:10,label:"业主方",display: '业主方'},
                20:{ value:20,label:"施工方",display: '施工方'},
                30:{ value:30,label:"监理方",display: '监理方'},
                40:{ value:40,label:"设计方",display: '设计方'},
                50:{value:50,label:"客户方",display: '客户方'}
                
            };
            if(response.data.result){
                this.props.form.setFormValue(response.data.result, { 
                    'role':selectOptions[ response.data.result.role].display,
                })

                this.modalTreeC.output.setSelectDataByDataSouce("personnelFiles_modalOrgTree_form_area", {
                   id:  response.data.result.pkOrgs,
                   name : response.data.result.orgNames === null? []:response.data.result.orgNames
                })

                if(! this.state.selectedTableData.length){
                    this.props.button.setButtonDisable(["Empower","UnEmpower" ], true)
                    this.props.button.setButtonDisable(["Empower","UnEmpower" ], true)
                }else{
                    this.props.button.setButtonDisable(["Empower","UnEmpower" ], false)
                    this.props.button.setButtonDisable(["Empower","UnEmpower" ], false)
                }

                this.setState({
                    tableData : response.data.result.personnelVOS || [] ,
                    columns :  this.state.columns,
                    initData : {...response.data.result ,tableData: response.data.result.personnelVOS || []},
                    pk : response.data.result.pkEnterprise,
                    deleteTableS :[],
                })
                success({"text":'保存数据成功'})
            
            }
            
        }else{
            danger({"text":response.data.exMsg});
        }
    })
}

function deleteCenterPrise(params){
    deleteCenterPriseReq(params).then((response)=>{
        if(response.data.resultCode != 'busi_ex'){
            
            if(this.state.status === "browse"){
                this.props.form.clearFormValue();
                this.modalTreeC.output.clearSelectDataByDataSouce("personnelFiles_modalOrgTree_form_area")
                this.setState({
                    tableData : [],
                    initData :{tableData :[]},
                    selectedTableData :[],
                })
                this.props.button.setButtonDisable(["Empower","UnEmpower" ,"Edit",'refresh, "Delete'], false)
            }else{
                console.log( this.props, this.props.search.getSearchData())
                let {
                    name,
                    pkSrc,
                    roles
                } = this.props.search.getSearchData().selectedS
                if(name || pkSrc.length ||roles.length){
                    let rolesA = [];
                    roles.roles.map((item)=>{
                        rolesA.push(item.key)
                    })
                    let params = {
                        name : record.name,
                        roles: rolesA,
                        pkOrgs: record.pkSrc,
                    }
                    buttonClick.call(this, "Search",params)
                }else{
                    buttonClick.call(this, "refresh")
                }
            }
            success({"text":'删除数据成功'})
        }else{
            danger({"text":response.data.exMsg});
        }
    })
}

function orgCreateBusiness(params){
    
    orgCreateBusinessReq(params).then((response)=>{
        if(response.data.resultCode != 'busi_ex'){
            buttonClick.call(this, "Edit",response.data.result)
        }else{
            danger({"text":response.data.exMsg});
        }
    })
}
function refersh (){
    refershReq({}).then((response)=>{
        if(response.data.resultCode != 'busi_ex'){
            response.data.result.map((item, index)=>{
                item["key"] = item.pkEnterprise
            })
            this.setState({
                tableData : response.data.result
            })
            success({"text":'查询数据成功'})
        }else{
            danger({"text":response.data.exMsg});
        }
    })
}

function queryCard (params, needToast = false){
    queryCardReq(params).then((response)=>{
        if(response.data.resultCode != 'busi_ex'){
            this["empower"] = false;
            let  selectOptions = {
                10 : {value:10,label:"业主方",display: '业主方'},
                20 : { value:20,label:"施工方",display: '施工方'},
                30 : { value:30,label:"监理方",display: '监理方'},
                40 : { value:40,label:"设计方",display: '设计方'},
                50 : {value:50,label:"客户方",display: '客户方'}
            };
         
            this.modalTreeC.output.setSelectDataByDataSouce("personnelFiles_modalOrgTree_form_area", {
                id:  response.data.result.pkOrgs,
                name :  response.data.result.orgNames === null? []:response.data.result.orgNames
            })

            this.setState({
                tableData : response.data.result ? response.data.result.personnelVOS :[],
                pk : response.data.result.pkEnterprise,
                initData : {...response.data.result ,tableData:response.data.result ? response.data.result.personnelVOS :[]},
                selectedTableData :[],
            })
            if(! this.state.selectedTableData.length){
                this.props.button.setButtonDisable(["Empower","UnEmpower" ], true)
                this.props.button.setButtonDisable(["Empower","UnEmpower" ], true)
            }else{
                this.props.button.setButtonDisable(["Empower","UnEmpower" ], false)
                this.props.button.setButtonDisable(["Empower","UnEmpower" ], false)
            }
            this.props.form.setFormValue(response.data.result, { 
                'role':selectOptions[ response.data.result.role].display,
            })
            this.state.status === "browse"&&needToast && success({"text":'查询数据成功'})
        }else{
            danger({"text":response.data.exMsg});
        }
    })
}

function empower (params){

    empowerReq(params).then((response)=>{
        
        if(response.data.resultCode != 'busi_ex'){
            queryCard.call(this, {pkEnterprise : params.personnelVOS[0].pkEnterprise}, false)//通过重新查询来刷新页面数据
            success({"text":`${params.onOff === 'off' ? "取消授权成功" : "授权成功"}`})
        }else{
            danger({"text":response.data.exMsg});
        }
    })
}


export {
    search,
    save,
    deleteCenterPrise,
    orgCreateBusiness,
    refersh,
    queryCard,
    empower
}